package com.yangtzeu.jwc.customview;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.zhangdi.jwc.R;

public class CustomeComfirmDialog implements android.view.View.OnClickListener {
    private Context context = null;
    private static Dialog customaddDialog = null;
    int dialogResult;
    public static Handler mHandler;

    public static boolean show(Context context, String title, String msg) {
	mHandler = new MyHandler();
	return new CustomeComfirmDialog().creat(context, title, msg).getResult() == 1;
    }

    public CustomeComfirmDialog creat(Context context, String title, String msg) {
	customaddDialog = new Dialog(context, R.style.theme_customer_progress_dialog);
	customaddDialog.setContentView(R.layout.custom_comfirm_dialog_layout);
	customaddDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
	customaddDialog.findViewById(R.id.btn_ok).setOnClickListener(this);
	customaddDialog.findViewById(R.id.btn_cancel).setOnClickListener(this);
	TextView tv_title = (TextView) customaddDialog.findViewById(R.id.dialog_title);
	TextView tv_msg = (TextView) customaddDialog.findViewById(R.id.comfirm_msg);
	tv_title.setText(title);
	tv_msg.setText(msg);
	customaddDialog.show();
	// 系统线程会阻塞
	try {
	    Looper.loop();
	} catch (Exception e) {
	}
	return this;
    }

    public int getResult() {
	return dialogResult;
    }

    private static class MyHandler extends Handler {
	@Override
	public void handleMessage(Message mesg) {
	    customaddDialog.dismiss();
	    throw new RuntimeException();
	}
    }

    public void onWindowFocusChanged(boolean hasFocus) {
	if (customaddDialog == null) {
	    return;
	}
    }

    @Override
    public void onClick(View v) {
	// TODO Auto-generated method stub
	int type = 0;
	switch (v.getId()) {
	case R.id.btn_ok:
	    type = 1;
	    break;
	case R.id.btn_cancel:
	    type = 0;
	    break;
	default:
	    break;
	}
	dialogResult = type;
	Message m = mHandler.obtainMessage();
	mHandler.sendMessage(m);
    }
}
